package com.ssm.forum.rpc.service.impl.assortment;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.core.annotation.BaseService;
import com.ssm.forum.dao.mapper.assortment.AssortmentDao;
import com.ssm.forum.dao.mapper.content.ContentDao;
import com.ssm.forum.dao.mapper.reply.ReplyDao;
import com.ssm.forum.dao.model.assortment.Assortment;
import com.ssm.forum.dao.model.content.Content;
import com.ssm.forum.rpc.api.assortment.AssortmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 帖子分类管理 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class AssortmentServiceImpl implements AssortmentService {

    @Resource
    private AssortmentDao assortmentDao;

    @Resource
    private ContentDao contentDao;

    @Resource
    private ReplyDao replyDao;


    /**
    * 保存数据
    * @param assortment 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(Assortment assortment) throws Exception {
        return assortmentDao.insert(assortment);
    }

    /**
    * 根据 id 修改
    * @param assortment 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(Assortment assortment) throws Exception {
        return assortmentDao.updateById(assortment);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteById(String id) throws Exception {
       List<Content> contents= contentDao.selectByAssortId(id);
       List<String> contentIds=new ArrayList<String>();
       if(contentIds.size()>0){
           for(Content c:contents){
               contentIds.add(c.getId());
           }
           replyDao.deleteByContentIds(contentIds);
           contentDao.deleteByAssortId(id);
       }
        return assortmentDao.deleteById(id);
    }

    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public Assortment selectById(String id) {
        return assortmentDao.selectById(id);
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<String> ids) throws Exception {
       List<Content> contents=contentDao.selectByAssortIds(ids);
        List<String> contentIds=new ArrayList<String>();
        for(Content c:contents){
            contentIds.add(c.getId());
        }
        replyDao.deleteByContentIds(contentIds);
        contentDao.deleteByAssortIds(ids);
        return assortmentDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    */
    public PageInfo<Assortment> list(Map map) {
        return assortmentDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    */
    public PageInfo<Assortment> list(Map map,int page,int size) {
        return assortmentDao.list(map,page,size);
    }


}
